//
//  SearchViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/15.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "SearchViewController.h"
#import "MineFirCell.h"
#import "SearchResultViewController.h"
@interface SearchViewController ()
<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    UITableView * _tableView;
    
    NSString * flag;
    NSMutableArray * btnArr;
    
    UITextField * textfield;
}
@end

@implementation SearchViewController
#pragma mark - 视图的生命周期
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

    
//    AppDelegate * app = [UIApplication sharedApplication].delegate;
//    [app.viewController isHiddenCustomTabBarByBoolean:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
//    
//    self.navigationItem.backBarButtonItem = backBarButton;
    
    

    
    [self main];
    [self addItems];
    [self addNavgationBar];
    // Do any additional setup after loading the view.
}
-(void)main
{
    flag = @"comp";
    btnArr = [[NSMutableArray alloc]init];
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 63, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableHeaderView = [self createHeaderView];
    [_tableView registerNib:[UINib nibWithNibName:@"MineFirCell" bundle:nil] forCellReuseIdentifier:@"MineFirCell"];
    [self.view addSubview:_tableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
}
#pragma mark - _tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineFirCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MineFirCell"];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
#pragma mark createHeaderView
-(UIView *)createHeaderView
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 75)];
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    
    CGFloat btnWidth = (SCREEN_WIDTH - SIZETO6W(15)*2 - SIZETO6W(25))/2;
    NSArray * titleArr = @[@"搜公司",@"搜职位"];
    for (int i = 0; i<2; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(SIZETO6W(15)+i*(btnWidth+SIZETO6W(25)), 15, btnWidth, 45);
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 20;
        [btnArr addObject:btn];

        [btn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
        [btn setBackgroundColor:UIColorFromRGB(0xFFFFFF)];
        btn.layer.borderColor = [UIColorFromRGB(0xdddddd) CGColor];
        btn.layer.borderWidth = 1.0f;
        btn.tag = 100+i;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            [btn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
            [btn setBackgroundColor:UIColorFromRGB(0x6ec844)];
            btn.layer.borderWidth = 0;
        }
        [view addSubview:btn];
    }
    
    return view;
}
-(UIView *)createFooterView
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SIZETO6H(50)+SIZETO6H(30)+40)];
    view.backgroundColor = UIColorFromRGB(0xf1f3f2);
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake((SCREEN_WIDTH-130)/2, SIZETO6H(50), 130, 40);
    [btn setTitle:@"清除搜索历史" forState:UIControlStateNormal];
    [btn setTitleColor:UIColorFromRGB(0x6ec844) forState:UIControlStateNormal];
    btn.layer.masksToBounds = YES;
    btn.layer.borderColor = [UIColorFromRGB(0x6ec844) CGColor];
    btn.layer.borderWidth = 1.0f;
    btn.layer.cornerRadius = 5;
    [view addSubview:btn];
    
    return view;
}
#pragma mark - btn点击事件们
-(void)btnAction:(UIButton *)btn
{
    int tag = (int)btn.tag;
    for (UIButton * b in btnArr) {
        if (b.tag == tag) {
            [b setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
            [b setBackgroundColor:UIColorFromRGB(0x6ec844)];
            b.layer.borderWidth = 0;
            
        }
        else
        {
            [b setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
            [b setBackgroundColor:UIColorFromRGB(0xFFFFFF)];
            b.layer.borderColor = [UIColorFromRGB(0xdddddd) CGColor];
            b.layer.borderWidth = 1.0f;
            
        }
    }

    if (tag == 100) {
        flag = @"comp";
    }
    else
    {
        flag = @"duty";
    }
    
    [textfield resignFirstResponder];
    SearchResultViewController * searchResultVC = [[SearchResultViewController alloc]init];
    searchResultVC.type = flag;
    searchResultVC.keyWord = textfield.text;
    [self.navigationController pushViewController:searchResultVC animated:YES];
}
-(void)textChange:(UITextField *)textField
{
    if ([textField.text isEqualToString:@"haha"]) {
        [self nosearch];
    }
    else
    {
        [self removeNosearch];
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([string isEqualToString:@"\n"]) {
        NSLog(@"开始搜索了");
        [textfield resignFirstResponder];
        SearchResultViewController * searchResultVC = [[SearchResultViewController alloc]init];
        searchResultVC.type = flag;
        NSLog(@"%@",textField.text);
        searchResultVC.keyWord = @"";
        [self.navigationController pushViewController:searchResultVC animated:YES];
        return NO;
    }
    return YES;
    
    
    
    
}

-(void)clearBtnAction:(UIButton *)btn
{
    UITextField * textField = (UITextField *)[self.view viewWithTag:713];
    textField.text = @"";
}
-(void)nosearch
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    view.backgroundColor = UIColorFromRGB(0xf1f3f2);
    view.tag = 504;
    
    UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-98/2,SIZETO6H(120), 98, 100)];
    imgV.image = [UIImage imageNamed:@"苦脸"];
    [view addSubview:imgV];
    
    UILabel * tipLab = [[UILabel alloc]initWithFrame:CGRectMake(0, imgV.bottom+SIZETO6H(20), SCREEN_WIDTH, 20)];
    tipLab.text = @"暂时没有符合条件的内容";
    tipLab.textAlignment = NSTextAlignmentCenter;
    tipLab.font = [UIFont systemFontOfSize:14];
    tipLab.textColor = UIColorFromRGB(0x333944);
    [view addSubview:tipLab];
    [self.view addSubview:view];
}
-(void)removeNosearch
{
    UIView * view = (UIView *)[self.view viewWithTag:504];
    [view removeFromSuperview];
}
#pragma mark - addItems
-(void)addItems
{
    UIButton * cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame = CGRectMake(0, 0, 60, 30);
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    [cancleBtn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc]initWithCustomView:cancleBtn];
    self.navigationItem.rightBarButtonItem = leftItem;
}
-(void)backItem:(id)sender
{
    [textfield resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - NAVgationBarTitle
-(void)addNavgationBar
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
    view.backgroundColor = UIColorFromRGB(0xf1f3f2);
    view.layer.masksToBounds = YES;
    
    UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(15), (35-20)/2, 20, 20)];
    imgV.image = [UIImage imageNamed:@"搜索"];
    [view addSubview:imgV];
    
    textfield = [[UITextField alloc]initWithFrame:CGRectMake(imgV.right + SIZETO6W(10), 0, view.frame.size.width-SIZETO6W(25)-20, 35)];
    [textfield becomeFirstResponder];
    UIColor * color = UIColorFromRGB(0xffffff);
    textfield.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"输入公司名或职位名" attributes:@{NSForegroundColorAttributeName:color}];
    [view addSubview:textfield];
    UIButton * clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    clearBtn.frame = CGRectMake(0, 0, 30, 30);
    [clearBtn setImage:[UIImage imageNamed:@"关闭按钮--拷贝"] forState:UIControlStateNormal];
    textfield.rightView = clearBtn;
    [textfield addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    textfield.keyboardType = UIKeyboardTypeWebSearch;
    textfield.delegate = self;
    [clearBtn addTarget:self action:@selector(clearBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    textfield.rightViewMode = UITextFieldViewModeWhileEditing;
    textfield.tag = 713;
    view.layer.cornerRadius = 18;
    self.navigationItem.titleView = view;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touch begin");
    self.editing = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
